package com.yh.correct.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.yh.correct.model.Education;
import com.yh.correct.model.Page;
import com.yh.correct.model.ResultData;
import com.yh.correct.service.IEducationService;
/**
 * 工作管理模块的教育基地控制层
 * @author Administrator
 */
@RestController
@RequestMapping("/education")
public class EducationController {
	private static Logger logger = LogManager.getLogger(EducationController.class);
	
	@Autowired
	private IEducationService educationService;
	
	/**
	 * 新增教育基地
	 * @param education
	 * @return
	 */
	@RequestMapping("/add")
	public ResultData addEducation(@Valid Education education){ 
		logger.info("新增教育基地"+education);
		int i=educationService.addEducation(education);
		ResultData rd=new ResultData();
		if(i>0){
			rd.setCode(0);
			rd.setMessage("增加成功！");
		}else{
			rd.setCode(-1);
			rd.setMessage("增加失败！");
		}
		return rd;
	}
	
	/**
	 * 删除教育基地
	 * @param education
	 * @return
	 */
	@RequestMapping("/del")
	public ResultData delEducation(Education education){ 
		logger.info("删除教育基地"+education);
		int i=educationService.delEducation(education);
		ResultData rd=new ResultData();
		if(i>0){
			rd.setCode(0);
			rd.setMessage("删除成功！");
		}else{
			rd.setCode(-1);
			rd.setMessage("删除失败！");
		}
		return rd;
	}
	
	/**
	 * 修改教育基地
	 * @param education
	 * @return
	 */
	@RequestMapping("/update")
	public ResultData updateEducation(@Valid Education education){ 
		logger.info("修改教育基地"+education);
		int i=educationService.updateEducation(education);
		ResultData rd=new ResultData();
		if(i>0){
			rd.setCode(0);
			rd.setMessage("修改成功！");
		}else{
			rd.setCode(-1);
			rd.setMessage("修改失败！");
		}
		return rd;
	}
	
	/**
	 * 根据id查询教育基地
	 * @param education
	 * @return
	 */
	@RequestMapping("/get")
	public ModelAndView getEducation(Education education){ 
		logger.info("查询教育基地"+education);
		education=educationService.getEducation(education);
		return new ModelAndView("work-manage/education_edit","edu",education);
	}
	
	
	/**
	 * 模糊查询教育基地
	 * @param pageSize
	 * @param pageNum
	 * @param education
	 * @return
	 */
	@RequestMapping("/querylike")
	public ResultData queryLikeEducations(int pageSize, int pageNum,Education education){ 
		logger.info("教育基地查询条件"+education);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pageSize", pageSize);
		map.put("pageNum", pageNum);
		map.put("education",education);
		Page page=educationService.queryLikeEducations(map);
		List<Object> list=new ArrayList<Object>();
		list.add(page);
		ResultData rd=new ResultData();
		rd.setCode(0);
		rd.setMessage("查询成功！");
		rd.setData(list);
		return rd;
	}
	
	
	/**
	 * 导出教育基地
	 * @param education
	 * @throws IOException 
	 */
	@RequestMapping("/export")
	public void exportEducations(Education education,HttpServletResponse response) throws IOException{ 
		logger.info("导出匹配的教育基地"+education);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("education",education);
		Workbook wb=educationService.exportEducations(map);
        response.setContentType("application/vnd.ms-excel;charset=utf-8");  
        String excelName = "教育基地";  
        response.setHeader("Content-Disposition","attachment; filename="+new String(excelName.getBytes("utf-8"), "ISO8859-1" )+".xls");  
		wb.write(response.getOutputStream());
	}
	
	/**
	 * 返回页面通用方法
	 * @param page
	 * @return
	 */
	@RequestMapping("/page")
	public ModelAndView page(String page){
		return new ModelAndView(page);
	}
	
}
